﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetworkModel;
using DLULearningNetwork.Libs;

namespace DLULearningNetwork.ActionFilterAttributes
{
    public class CheckClassOwnerIsNotBanned : ActionFilterAttribute
    {
        public Guid CourseID { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                if (filterContext.ActionParameters["id"] != null)
                {
                    this.CourseID = Guid.Parse(filterContext.ActionParameters["id"].ToString());
                }
                else
                {
                    this.CourseID = Guid.Parse(filterContext.ActionParameters["courseID"].ToString());
                }
            }
            catch
            { 
                
            }

            if (this.CourseID != null && this.CourseID != Guid.Empty)
            {
                Course course = Course.GetCourseByID(this.CourseID);
                if (course != null)
                {
                    Account account = Account.Single(course.AccountID_u);
                    if (account.IsBanned_b.HasValue && account.IsBanned_b.Value)
                    {
                        LibSession.Set(Constants.ERROR_MESSAGE, "Lớp học này hiện tại không thể truy cập");
                        filterContext.Result = new RedirectResult("/Course/Error");
                    }
                }
            }

            base.OnActionExecuting(filterContext);
        }        
    }
}